package com.gold.controller;

import com.alibaba.fastjson.JSONObject;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgMaterialOptionalRequest;
import com.taobao.api.request.TbkDgOptimusMaterialRequest;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


/**
 * @author XiaoHao
 * @date 2022-04-13 09:53
 */
@Controller
@RequestMapping("api/taobao")
public class TbController {
    private TaobaoClient client = new DefaultTaobaoClient(
            "http://gw.api.taobao.com/router/rest",
            "33660200",
            "aa3b2f22fd362f7441819e027459d7a8");

    /**
     * 物料精选
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("materialOptional")
    @ResponseBody
    public String query(@RequestBody JSONObject parm) {
        try {

            TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
            Object materialId = parm.get("material_id");
            Object pageSize = parm.get("page_size");
            Object pageNo = parm.get("page_no");
            if (pageNo != null) {
                req.setPageNo(Long.valueOf(pageNo.toString()));
            }
            if (pageSize != null) {
                req.setPageSize(Long.valueOf(pageSize.toString()));
            }
            if (materialId != null) {
                req.setMaterialId(Long.valueOf(materialId.toString()));
            }
            req.setAdzoneId(112006400416L);
            TbkDgOptimusMaterialResponse rsp = client.execute(req);
            return rsp.getBody();
        } catch (Exception e) {
            return "后台错误,请联系管理员";
        }
    }

    private static  Long num =1L;
    @RequestMapping("test")
    @ResponseBody
    public HashMap<String, String> test() {
        HashMap<String, String> map = new HashMap<>();

        try {

            TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
            req.setPageNo(num);
            req.setPageSize(1L);
            req.setMaterialId(44412L);
            req.setAdzoneId(112006400416L);
            TbkDgOptimusMaterialResponse rsp = client.execute(req);
            List<TbkDgOptimusMaterialResponse.MapData> resultList = rsp.getResultList();
            for (TbkDgOptimusMaterialResponse.MapData mapData : resultList) {
                //标题
                String title = mapData.getTitle();
                //连接
                String clickUrl = mapData.getClickUrl();
                //图片
                String pictUrl = mapData.getPictUrl();
                if (pictUrl!=null&&clickUrl!=null&&title!=null){
                    num++;
                    map.put("title",title);
                    map.put("clickUrl",clickUrl);
                    map.put("pictUrl",pictUrl);
                    return map;
                }else {
                    num++;
                }
            }
            map.put("error","无数据");
            return map;
        } catch (Exception e) {
            map.put("error","后台错误");
            return map;
        }
    }

    /**
     * 物料搜索
     *
     * @param parm
     * @return
     * @throws Exception
     */
    @RequestMapping("materialSearch")
    @ResponseBody
    public String querys(@RequestBody JSONObject parm) {
        try {

            TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
            Object parm1 = parm.get("q");
            Object pageSize = parm.get("page_size");
            Object pageNo = parm.get("page_no");
            Object hasCoupon = parm.get("has_coupon");
            if (parm1 != null) {
                req.setQ(parm1.toString());
            }
            if (pageNo != null) {
                req.setPageNo(Long.valueOf(pageNo.toString()));
            }
            if (pageSize != null) {
                req.setPageSize(Long.valueOf(pageSize.toString()));
            }
            if (hasCoupon != null) {
                String s = hasCoupon.toString();
                req.setHasCoupon("true".equals(s) ? true : false);
            }
            req.setAdzoneId(112006400416L);
            TbkDgMaterialOptionalResponse rsp = client.execute(req);
            return rsp.getBody();
        } catch (Exception e) {
            return "后台错误,请联系管理员";
        }

    }


}
